<?php

namespace app\admin\controller\post;

use app\common\controller\Backend;
use think\process\exception\Failed;

// 用户控制器
class Post extends Backend{
    // 构造函数
    public function __construct()
    {
        parent::__construct();

        // 全局
        $this->PostModel=model('Post.Post');
        $this->CateModel=model('Post.Cate');
    }

    // 用户列表
    public function index(){
        // 接收关键词
        $keywords=$this->request->param('keywords','');
        $start=$this->request->param('start','');
        $end=$this->request->param('end','');

        // 封装一个查询条件的数值
        $where=[];

        if(!empty($keywords)){
            $where['title']=['like',"%$keywords%"];
        }

        // 判断开始时间是否为空
        if(!empty($start)){
            // 标准时间转化为时间戳
            $starttime=strtotime($start);
            $where['pre_post.createtime']=['EGT',$starttime];
        }

        // 判断结束时间是否为空
        if(!empty($end)){
            // 标准时间转化为时间戳
            $endtime=strtotime($end);
            $where['pre_post.createtime']=['ELT',$endtime];
        }

        // 当开始时间和结束时间都不为空时
        if(!empty($start) && !empty($end))
        {
            $starttime=strtotime($start);
            $endtime=strtotime($end);
            $where['pre_post.createtime']=['between',[$starttime,$endtime]];
        }

        // 总数
        $count=$this->PostModel->where($where)->count();

        $postlist=$this->PostModel->with(['cate','user'])->order('createtime','desc')->where($where)->paginate(10);

        // 分页调用
        $pagestr=$postlist->render();

        // 赋值
        $this->assign([
            'count'=>$count,
            'postlist'=>$postlist,
            'pagestr'=>$pagestr,
            'keywords'=>$keywords,
            'start'=>$start,
            'end'=>$end,
        ]);

        return $this->fetch();
    }

    public function cate(){
        $catelist=$this->CateModel->order('weigh','asc')->paginate(10);

        $count=$this->CateModel->count();

        $pagestr=$catelist->render();

        $this->assign([
            'catelist'=>$catelist,
            'pagestr'=>$pagestr,
            'count'=>$count,
        ]);

        return $this->fetch();
    }

    public function cateedit(){
        $cateid=$this->request->param('id',0);
        $catelist=$this->CateModel->where(['id'=>$cateid])->find();

        if($this->request->isPost()){
            $cate=$this->request->param();

            $name=$this->CateModel->where('name',$cate['name'])->where('id', '<>' ,$cateid)->find();
            if($name){
                $this->error('该分类已存在，请重新输入');
                exit;
            }

            if(empty($cate['weigh'])){
                $this->error('请设定该分类的权重值');
                exit;
            }

            $data=[
                'id'=>$cateid,
                'name'=>$cate['name'],
                'weigh'=>$cate['weigh'],
            ];

            $success=$this->CateModel->isUpdate(true)->save($data);

            if($success === FALSE){
                $this->error($this->CateModel->getError());
                exit;
            }

            $this->success('修改成功',url('admin/post.post/cate'));
            exit;
        }

        $this->assign([
            'catelist'=>$catelist,
        ]);

        return $this->fetch();
    }

    public function cateadd(){
        if($this->request->isPost()){
            $cate=$this->request->param();

            $result=$this->CateModel->where(['name'=>$cate['name']])->find();
            if($result){
                $this->error('该分类已存在，请重新命名');
                exit;
            }

            if(empty($cate['weigh'])){
                $this->error('请设定该分类的权重值');
                exit;
            }

            $data=[
                'name'=>$cate['name'],
                'weigh'=>$cate['weigh'],
            ];

            $success=$this->CateModel->save($data);

            if($success === FALSE){
                $this->error($this->CateModel->getError());
                exit;
            }

            $this->success('添加分类成功',url('admin/post.post/cate'));
            exit;
        }

        return $this->fetch();
    }

    // 删除
    public function del()
    {
        $success = [
            'result' => false,
            'msg' => ''
        ];

        if($this->request->isAjax())
        {
            $ids = $this->request->param('ids',0);
            
            $result = $this->PostModel->destroy($ids);

            if($result === FALSE){
                $success['result'] = false;
                $success['msg'] = $this->PostModel->getError();
            }else
            {
                $success['result'] = true;
                $success['msg'] = '删除成功';
            }
            return json($success);
        }
    }

    // 删除
    public function catedel()
    {
        $success = [
            'result' => false,
            'msg' => ''
        ];

        if($this->request->isAjax())
        {
            $ids = $this->request->param('ids',0);

            $result = $this->CateModel->destroy($ids);

            if($result === FALSE){
                $success['result'] = false;
                $success['msg'] = $this->CateModel->getError();
            }else
            {
                $success['result'] = true;
                $success['msg'] = '删除分类列表成功';
            }
            return json($success);
        }
    }
}